<template>
  <yy-paging v-model="dataList" @query="queryList" ref="paging" :showMoreNoMore="false" :auto="false" @scroll="scroll" :refresher-enabled="false">
    <template #top>
      <u-navbar
        back-text=""
        title="筛选鱼种"
        :background="{ backgroundColor: !isScroll ? '#FFFFFF' : 'transparent' }"
        :border-bottom="false"
        :custom-style="{ transition: 'background-color 0.3s ease-in-out' }"
      >
      </u-navbar>
    </template>
    <view class="flex flex-col gap-4 p-4 bg-white">
      <view class="text-[14px] text-[#171B24]"> 钓点可钓鱼种</view>
      <view class="grid grid-cols-3 gap-4">
        <view
          class="col-span-1 flex justify-center items-center rounded-[18px] p-2 border border-solid"
          v-for="(tag, index) in tags"
          :key="index"
          :class="selectedTags.includes(tag.id) ? 'bg-[#00A9AB] text-white' : 'bg-[#FFFFFF] text-[#171B24]'"
          @click="toggleTag(index, tag)"
        >
          <text class="text-[12px] line-clamp-1">{{ tag.name }}</text>
        </view>
      </view>
      <view> </view>
    </view>
    <template #bottom>
      <view class="px-4 py-3 bg-white">
        <view class="grid grid-cols-3 h-[44px] gap-4">
          <view class="col-span-1 bg-[#F4F6F8] rounded-full flex items-center justify-center" @click="reset">
            <text class="text-[16px] text-black">重置</text>
          </view>
          <view class="bg-[#00A9AB] rounded-full flex items-center justify-center col-span-2" @click="submit">
            <text class="text-[16px] text-[#E5F6F6]">提交</text>
          </view>
        </view>
      </view>
    </template>
  </yy-paging>
</template>

<script>
  export default {
    data() {
      return {
        isScroll: false,
        dataList: [],
        automatic: false,
        tagId: '',
        tags: [],
        selectedTags: [] // 存储选中的标签索引
      }
    },
    onLoad(options = {}) {
      const eventChannel = this.getOpenerEventChannel()
      if (eventChannel.on) {
        eventChannel.on('data', data => {
          this.tags = data
        })
      }
    },

    methods: {
      submit() {
        const eventChannel = this.getOpenerEventChannel()
        if (eventChannel.emit) {
          eventChannel.emit('update', { selectedTags: this.selectedTags })
        }
        vk.navigateBack()
      },
      reset() {
        this.selectedTags = []
      },
      toggleTag(index, tag) {
        const tagId = tag.id
        const tagIndex = this.selectedTags.indexOf(tagId)
        if (tagIndex > -1) {
          this.selectedTags.splice(tagIndex, 1)
        } else {
          this.selectedTags.push(tagId)
        }
      },
      scroll(e) {
        this.isScroll = e.detail.scrollTop > 0
      },

      async queryList(page, limit) {
        this.$refs.paging.complete([1, 1, 1])
      }
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #ffffff !important;
  }
</style>
